package com.ruoyi.activiti.listener;

import com.alibaba.fastjson.JSON;
import com.ruoyi.activiti.bpmnparse.x6translator.X6Helper;
import com.ruoyi.activiti.domain.CustomProcess;
import com.ruoyi.activiti.mapper.CustomProcessMapperHis;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.impl.persistence.entity.VariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@Component
public class MultiExecutionListener implements ExecutionListener {
    // 1按部门，2按角色，3按用户
    private final static String BY_DEPT = "1";
    private final static String BY_ROLE = "2";
    private final static String BY_USER = "3";
    @Autowired
    private CustomProcessMapperHis customProcessMapperHis;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public void notify(DelegateExecution execution) {
        System.out.println("start");
        List<String> users = (List<String>) execution.getVariable("assigneeList");
        if(users == null || users.size() == 0) {
            CustomProcess customProcess = customProcessMapperHis.selectByProcDefId(execution.getProcessDefinitionId());
            String graphData = customProcess.getGraphData();
            Map<String,Object> graphDataMap = JSON.parseObject(graphData);
            X6Helper x6Helper = new X6Helper();
            List<Map<String,Object>> listMap = (List<Map<String, Object>>) graphDataMap.get("cells");
            Map<String,Object> dataMap = x6Helper.getBpmnDataById(listMap,execution.getCurrentActivityId());
            String userType = (String) dataMap.get("multiUserType");
            String userData = (String) dataMap.get("users");
            if(BY_ROLE.equals(userType)) {
                users = getUsersByRole(userData);
            } else if(BY_USER.equals(userType)) {
                users = getUsers(userData);
            }
            if(!CollectionUtils.isEmpty(users)) {
                users = users.stream().distinct().collect(Collectors.toList());
                execution.setVariable("assigneeList", users);
            }
        }
    }
    private List<String> getUsersByRole(String userData){
        List<SysUser> resultList = new ArrayList<>();
        if(userData != null) {
            List<String> roleList = Arrays.asList(userData.split(","));
            for (String role : roleList) {
                List<SysUser> users = sysUserMapper.selectUserListByRoleKey(role);
                resultList.addAll(users);
            }
            return resultList.stream().map(SysUser::getUserName).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }
    private List<String> getUsers(String userData){
        if(userData != null) {
            String[] userArr = userData.split(",");
            return Arrays.asList(userArr);
        } else {
            return new ArrayList<>();
        }
    }
}
